using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;

namespace Platform.algorithm
{
    class KnowledgeBase_DB : KnowledgeBase
    {
        public KnowledgeBase_DB()
        {
            getAllKnowledge();
        }
        protected override void getAllKnowledge()
        {
            //string standar = GlobalSetting.KnowledgeSet == KONWLEDGESET.GB18030 ? "GB18030" : "GB2312";
            //string file = GlobalSetting.KnowledgeSet == KONWLEDGESET.GB18030 ? "knowledge1\\kwg_18030.txt" : "knowledge1\\kwg_6763.txt";
            string file = "knowledge1\\kwg_18030.txt";
            string knowledgeTxt = "";
            string str = "Data Source=HHH\\SQLEXPRESS;Initial Catalog=worddb;Integrated Security=True;";
            SqlConnection connection = new SqlConnection(str);
            string commandtxt = "SELECT * FROM words WHERE standar='27533'";
            SqlCommand command = new SqlCommand(commandtxt, connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            try
            {
                DataSet set = new DataSet();
                connection.Open();
                adapter.Fill(set);
                adapter.Dispose();
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    DataRow row = set.Tables[0].Rows[i];
                    string wordid = row["wordid"].ToString().Trim();
                    SqlDataAdapter ada = new SqlDataAdapter("SELECT * FROM params WHERE wordid=" + wordid + " ORDER BY orderid ASC", connection);
                    DataSet set2 = new DataSet();
                    ada.Fill(set2);
                    ada.Dispose();
                    string line = row["ecode"].ToString().Trim() + " " + row["icode"].ToString().Trim();
                    for (int j = 0; j < set2.Tables[0].Rows.Count; j++)
                    {
                        DataRow row2 = set2.Tables[0].Rows[j];
                        string param = row2["para"].ToString().Trim();
                        string[] vals = param.Split(',');
                        for (int k = 0; k < vals.Length; k++)
                        {
                            line += " " + vals[k];
                        }
                    }
                    line += " " + row["gbk"].ToString().Trim() + " " + row["frequency"].ToString().Trim() + " " + row["pinyin"].ToString().Trim() + "\r\n";
                    knowledgeTxt += line;
                    if(i % 2000 == 0)
                        System.Threading.Thread.Sleep(1000);
                }
                set.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return;
            }
            finally
            {
                connection.Close();
            }
            FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
            byte[] data = Encoding.ASCII.GetBytes(knowledgeTxt);
            fs.Write(data, 0, data.Length);
            fs.Close();
            fs.Dispose();
        }
    }
}
